<!--
* description: 不良品登记新增
* author:xuliwen
* createdDate:2021-2-3
-->
<template>
  <el-dialog
    v-dialogDrag
    :visible="popupsVisible"
    width="70%"
    :append-to-body="true"
    :close-on-click-modal="false"
    @close="closeEdit"
  >
    <dialogHeader
      slot="title"
      :title="titleName"
      :style-type="$store.state.app.dialogType"
    />
    <!-- 显示按钮的onetabel -->
    <one-table-template
      ref="multipleTable"
      :dynamic-buttons="tableButtons"
      :dynamic-components="tableComponents"
      :dynamic-api-config="apiConfig"
      :dynamic-table-cols="tableCols"
      :dynamic-form-fields="formField"
      :dynamic-is-init-table="true"
      :dynamic-is-show-more-btn="false"
      :callback-obj="callbackObj"
    />
  </el-dialog>
</template>

<script>
import { oneTableWithViewTemplateMixins } from '@/components/mixins/oneTableWithViewTemplateMixins'
import dialogHeader from '@/components/dialogHeader'
import OneTableTemplate from '@/components/templates/oneTable'
import request from '@/utils/request'
import api from '@/api/pa/apiBranchWarehouse'
export default {
  components: {
    dialogHeader,
    OneTableTemplate
  },
  // 组件混入对象
  mixins: [oneTableWithViewTemplateMixins],
  props: {
    popupsVisible: {
      type: Boolean,
      default: false
    },
    dataForForm: {
      type: Object,
      default: () => {}
    }
  },
  data() {
    // 页面标识（与上面beforeRouteEnter中的对应）
    const viewPageCode = this.$route.path
    // 绑定事件配置化事件
    this.$ConfigCache.CacheConfig.bindEvent(this, [viewPageCode])
    return {
      selectiondata: [],
      editPopupsVisible: false,
      titleName: '不良品新增',
      // 标题
      textMap: {
        edit: '配件编辑',
        add: '配件新增'
      },
      // 网格查询API配置对象
      apiConfig: api.insertQuery,
      // 动态组件-按钮
      tableButtons: [
        {
          compKey: 'btnKey1',
          type: 'primary',
          size: 'small',
          name: 'search',
          clickEvent: () => this.queryTable(1),
          text: this.$t('sys.button.query') /* 保存*/,
          position: 'right'
        },
        {
          compKey: 'btnKey2',
          type: '',
          size: 'small',
          icon: 'iconfont  icon-cancel',
          clickEvent: () => this.choosePart(),
          text: '保存' // 取消
        },
        {
          compKey: 'btnKey2',
          type: '',
          size: 'small',
          name: 'reset',
          clickEvent: () => this.reset(),
          text: this.$t('sys.button.reset'),
          position: 'right' // 重置
        }
      ],
      // 动态组件-查询条件
      tableComponents:
        this.$ConfigCache.CacheConfig.cacheData[viewPageCode] &&
        this.$ConfigCache.CacheConfig.cacheData[viewPageCode].formComponents
          .length > 0
          ? this.$ConfigCache.CacheConfig.cacheData[viewPageCode].formComponents
          : [
            {
              compKey: 'compKey1',
              labelName: '仓库名称：',
              codeField: 'warehouseId',
              component: () => import('@/components/org/commonSelect'),
              textField: 'warehouseName',
              customObj: {
                api: api.queryWarehouseNameSelect,
                defOptionFields: ['warehouseId', 'warehouseName']
              },
              type: 'dropdownList',
              isMul: false,
              isMust: true
            },
            {
              compKey: 'compKey2',
              labelName: '备件编码：',
              codeField: 'partNo',
              component: () => import('@/components/org/commonInput'),
              type: 'inputText',
              span: 8,
              isMust: true
            },
            {
              compKey: 'compKey3',
              labelName: '备件名称：',
              codeField: 'partName',
              component: () => import('@/components/org/commonInput'),
              type: 'inputText',
              span: 8,
              isMust: true
            }
          ],
      // 动态生成网格列
      tableCols:
        this.$ConfigCache.CacheConfig.cacheData[this.$route.path] &&
        this.$ConfigCache.CacheConfig.cacheData[this.$route.path].tableCols &&
        this.$ConfigCache.CacheConfig.cacheData[this.$route.path].tableCols
          .length > 0
          ? this.$ConfigCache.CacheConfig.cacheData[this.$route.path].tableCols
          : [
            {
              prop: 'partId',
              label: '备件ID',
              hidden: true,
              alwaysHidden: true
            },
            {
              prop: 'storageId',
              label: 'storageId',
              hidden: true,
              alwaysHidden: true
            },

            {
              prop: 'updateControlId',
              label: '并发控制ID',
              hidden: true,
              alwaysHidden: true
            },
            {
              prop: 'warehouseId',
              label: '仓库ID',
              hidden: true,
              alwaysHidden: true
            },
            {
              prop: 'warehouseName',
              label: '仓库名称',
              width: '150px'
            },
            {
              prop: 'partNo',
              label: '备件编号'
            },
            {
              prop: 'partName',
              label: '备件名称'
            },

            {
              prop: 'accountQty',
              label: '账面库存'
            },
            {
              prop: 'badProductsQty',
              label: '不良品'
            },
            {
              prop: 'productsQtyAS',
              label: '良品'
            },
            {
              prop: 'toAbleQtr',
              label: '不良品转良品+',
              width: '100px',
              isComponent: true,
              comps: [
                {
                  compKey: 'compKey5' + this.$utils.generateId(),
                  codeField: 'toAbleQtr',
                  component: () =>
                    import('@/components/org/commonInput/numberInput.vue'),
                  isInput: true,
                  dataToObject: {
                    min: 0,
                    empty: true
                  },
                  decPoint: 0,
                  step: 1
                }
              ]
            },
            {
              prop: 'toUnableQty',
              label: '良品转不良品+',
              width: '100px',
              isComponent: true,
              comps: [
                {
                  compKey: 'compKey5' + this.$utils.generateId(),
                  codeField: 'toUnableQty',
                  component: () =>
                    import('@/components/org/commonInput/numberInput.vue'),
                  isInput: true,
                  dataToObject: {
                    min: 0,
                    empty: true
                  },
                  decPoint: 0,
                  step: 1
                }
              ]
            },
            {
              prop: 'remark',
              label: '备注+',
              isComponent: true,
              comps: [
                {
                  compKey: 'compKey5' + this.$utils.generateId(),
                  codeField: 'remark',
                  component: () => import('@/components/org/commonInput'),
                  // isInput: true,
                  // dataToObject: {
                  //   empty: true,
                  // },
                  type: 'inputText'
                }
              ]
            },
            {
              prop: 'applyer',
              label: '登记人+',
              isComponent: true,
              comps: [
                {
                  compKey: 'compKey5' + this.$utils.generateId(),
                  codeField: 'applyer',
                  component: () => import('@/components/org/commonInput'),
                  isInput: true,
                  dataToObject: {
                    empty: true
                  },
                  type: 'inputText'
                }
              ]
            },
            {
              prop: 'applyDate',
              label: '登记时间+',
              width: '150px',
              isComponent: true,
              comps: [
                {
                  compKey: 'compKey5' + this.$utils.generateId(),
                  codeField: 'applyDate',
                  component: () => import('@/components/org/commonInput'),
                  isInput: true,
                  dataToObject: {
                    empty: true
                  },
                  type: 'inputText'
                }
              ]
            }
          ],
      callbackObj: {
        onCheckSelectChange: (selection) => {
          this.selectiondata = selection
        }
      },
      formField: {
        warehouseId: '',
        partNo: '',
        partName: ''
      }
    }
  },

  methods: {
    // 关闭弹窗
    closeEdit(type) {
      this.$emit('close', type)
    },
    // 选择保存
    choosePart() {
      if (this.selectiondata.length > 0) {
        request({
          url: api.insertRejects.APIUrl,
          method: 'post',
          data: this.selectiondata
        }).then((res) => {
          if (res.result === '1') {
            this.$message.success(res.msg || '保存成功!')
            this.popupsVisible = false
            this.$nextTick(() => {
              this.$parent.queryTable(1)
            })
          } else {
            this.$message(res.msg || '保存失败！')
          }
        })
      } else {
        this.$message.warning('请选择')
      }
    }
  }
}
</script>
<style lang='scss' scoped>
/deep/ .el-dialog {
  margin-top: 5rem !important;
}
</style>
